<?php

namespace addons\helper\controller\wxapp\user;

use addons\helper\controller\wxapp\Base;
use fast\Tree;

class Group extends Base
{
    protected $noNeedRight = ['ruletree'];
    protected $config = [
        'model'             => '会员组',
        'limit'             => 0,
        'defaultSort'       => 'id',
        'defaultOrder'      => 'desc',
        'tabField'          => '',
        'multi'             => true,
        'ruletreeUrl'       => 'user.group/ruletree',
        'field' => [
            'name'       => ['组名', 'text', false,  false, '', true, true, true],
            'rules'      => ['权限', 'ruletree', false,  false, '', true, true, true],
            'createtime' => ['添加时间', 'datetime', false, false, '', true, false, false],
            'updatetime' => ['更新时间', 'datetime', false, false, '', true, false, false],
            'status'     => ['状态', 'select', false, false, '', true, true, true],
        ],
        'fieldContent' => [
            'status' => [
                'normal' => '正常',
                'hidden' => '隐藏'
            ]
        ],
        // [可选]前端表单验证规则
        'rules' => [
            'name' => [
                'required'    => true
            ],
            'rules' => [
                'required'    => true
            ]
        ],
        // [可选]前端表单自定义验证提示信息
        'rulesMessage' => [
            'rules' => [
                'required'    => '必须选择权限'
            ]
        ],
        'auth' => [
            'add'  => [
                'name' => '添加',
                'url'  => 'user.group/add',
            ],
            'edit'  => [
                'name' => '编辑',
                'url'  => 'user.group/edit',
            ],
            'multi' => [
                'del'  => [
                    'name'        => '删除',
                    'color'       => '#e74c3c',
                    'icon'        => 'fa-trash',
                    'url'         => 'user.group/del'
                ],
                'hidden'  => [
                    'name'        => '设为隐藏',
                    'color'       => '#f39c12',
                    'icon'        => 'fa-eye-slash',
                    'url'         => 'user.group/multi',
                    'params'      => 'status=hidden'
                ],
                'normal'  => [
                    'name'        => '设为正常',
                    'color'       => '#18bc9c',
                    'icon'        => 'fa-eye',
                    'url'         => 'user.group/multi',
                    'params'      => 'status=normal'
                ],
            ],
            'more' => [
                'del'  => [
                    'name'  => '删除',
                    'color' => '#e74c3c',
                    'url'   => 'user.group/del'
                ],
                'hidden'  => [
                    'name'        => '设为隐藏',
                    'color'       => '#f39c12',
                    'url'         => 'user.group/multi',
                    'params'      => 'status=hidden'
                ],
                'normal'  => [
                    'name'        => '设为正常',
                    'color'       => '#18bc9c',
                    'url'         => 'user.group/multi',
                    'params'      => 'status=normal'
                ],
            ],
        ],
    ];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\UserGroup;
        $this->config['field'] = $this->formatField();
        $this->config['pk'] = $this->model->getPk() ? $this->model->getPk() : 'id';
    }

    public function edit()
    {
        $ids = $this->request->get('ids');
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {
            return parent::edit();
        }
        $this->success('ok',$row);
    }
    /**
     * 读取角色权限树
     *
     * @internal
     */
    public function ruletree()
    {
        $ruleList = collection(model('app\admin\model\UserRule')->where('status', 'normal')->order('weigh desc,id desc')->select())->toArray();
        foreach ($ruleList as $k => $v) {
            $ruleList[$k]['title'] = __($v['title']);
        }
        $ruleTree = new Tree();
        $ruleTree->init($ruleList,null,' ');
        $this->success('',$ruleTree->getTreeArray(0));
    }

}
